<?php 

require_once 'config.php';
$dbConn = mysql_pconnect (DBHOST,DBUSER,DBPASS) or die ('MySQL connect failed. ' . mysql_error());
mysql_select_db(DBNAME);
$result=mysql_query("select * from `schedules`");
$i=0;

#$interval = "0,15,30,45"; //Update cron_replace.txt every 15 mins
$interval = "40,45"; //Update cron_replace.txt every 15 mins

$HOME = "/home/rsoni/public_html";

$myFile = "$HOME/cron_replace.txt";
$fh = fopen($myFile, 'w') or die("can't open file");

$chmod_commment = "#Change permissions to create file\n";
fwrite($fh, $chmod_commment);
$chmod = "$interval * * * * chmod 777 -R $HOME 2>&1\n";
fwrite($fh, $chmod);

$start_comment = "#Following line runs scan_schedules.php regularly \n";
fwrite($fh, $start_comment);
$start = "$interval * * * * /usr/bin/php /home/rsoni/public_html/scan_schedules.php 2>&1\n";
fwrite($fh, $start);

$command_comment = "#Add schedule commands below\n";
fwrite($fh, $command_comment);

//Write all schedule commands
while($row=mysql_fetch_row($result))
{
//$row[1]; //*/1 * * * *
//$row[2] //home/rsoni/public_html/trigger.php&type=svn

//example */1 * * * * /usr/bin/php /home/rsoni/public_html/CronScheduler.php svn

$time = $row[1];
$php = ' /usr/bin/php ';
$url = substr($row[2],0,strrpos($row[2], "&")).' ';
$argument = substr($row[2],strrpos($row[2], "=")+1,strlen($row[2])-1).' ';

$command = $time.$php.$url.$argument." 2>&1\n";
fwrite($fh, $command);

}

//Change permission of cron_replace.txt
$chmod_commment1 = "#Change permissions of cron_replace.txt\n";
fwrite($fh, $chmod_commment1);
fwrite($fh, $chmod);

$last_comment = "#Replace crontab with cron_replace.txt to get the updated crontab file\n";
fwrite($fh, $last_comment);
$last = "$interval * * * * crontab $HOME/cron_replace.txt 2>&1";
fwrite($fh, $last);

?>